jQuery(document).ready(function($) {
    // 注册
    Vue.filter('date', function(value) {
        if (!value) return '';
        return moment(value).format();
    });
    var vm = new Vue({
        el: '#app',
        data: {
            users: [],
            dash: [],
            search: ''
        },
        methods: {
            createHandler: function(event) {
                var _this = this;
                $.post('/api/create', {
                    name: 'test-' + (new Date().getTime())
                }, function(data, textStatus, xhr) {
                    // $('#console').append($('<pre/>').html(JSON.stringify(data, null, 2)));
                    _this.refreshHandler();
                });
            },
            refreshHandler: function(event) {
                var _this = this;
                $.get('/api/list', {}, function(data, textStatus, xhr) {
                    _this.users = _.values(data);
                });
                $.get('/api/dash', {}, function(data, textStatus, xhr) {
                    _this.dash = data;
                });
            },
            deleteHandler: function(item) {
                var _this = this;
                $.post('/api/delete/' + item.userInfo.id, {}, function(data, textStatus, xhr) {
                    // $('#console').append($('<pre/>').html(JSON.stringify(data, null, 2)));
                    _this.refreshHandler();
                });
            },
            updateHandler: function(item) {
                var _this = this;
                $.post('/api/update/' + item.userInfo.id, {}, function(data, textStatus, xhr) {
                    // $('#console').append($('<pre/>').html(JSON.stringify(data, null, 2)));
                    _this.refreshHandler();
                });
            },
            searchHandler: function() {
                var _this = this;
                if (!this.search) {
                    return;
                }
                $.get('/api/get/' + this.search, {}, function(data, textStatus, xhr) {
                    if (data) {
                        _this.users = [{ userInfo: data[0], nodeInfos: [data[1]] }];
                    } else {
                        _this.users = [];
                    }
                });
            },
            searchByIdHandler: function(item) {
                var _this = this;
                this.search = item.userInfo.id;
                this.searchHandler();
            }
        },
        created: function() {
            this.refreshHandler();
        }
    });

});
